package unique;

//import backup.*;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class GUIClient extends javax.swing.JPanel {

    private int idCounter = 0;
    private DefaultListModel listModel;
    private WebClient serverConnect;
    private FileHandler collection;
    boolean botao = false;

    private int guiSelected;

    public GUIClient() {
        //inicialização da lista de cartas
        listModel = new DefaultListModel();
        //listModel.addElement("Aperte no botão atualizar");
        //serverConnect = new WebClient("localhost", 6789, "archiveClient.txt");
        initComponents();
        guiAtualizaLista.setEnabled(false);
        guiApagar.setEnabled(false);
        guiSalvar.setEnabled(false);
        
        //atualizaLista();
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Cliente"); //pode editar aqui pra algo legal
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new GUIClient();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        
    }

    private void atualizaLista() {
        botao = true;

        listModel.clear();
        listModel.addElement("Novo");
        System.out.println("depois do clear: " + listModel.size());
        String aux = "";
        serverConnect.resetCardsList();
        serverConnect.resetFile();
        serverConnect.cardIndex();
        collection = serverConnect.getCardList();
        //System.out.println("botão atualiza");
        //collection.testPrint();
        System.out.println("Test print:");
        collection.testPrint();
        int size = collection.getCardsList().size();
        System.out.println("Size: " + size);
        //System.out.println("botão atualiza for 1");
        try {
            for (int i = 0; i < size; i++) {
                aux = collection.getCardByIdLL(i).getName();
                System.out.println("aux: " + aux);
                listModel.addElement(aux);
                System.out.println("depois do clear: " + listModel.size());
            }
        } catch (Exception e) {
            System.out.println("né");
        }

        botao = false;
    }

    private void limpaCampos() {
        guiCMC.setText(null);
        guiCost.setText(null);
        guiDescricao.setText(null);
        guiNome.setText(null);
        guiPoder.setText(null);
        guiRaridade.setText(null);
        guiResistencia.setText(null);
        guiSet.setText(null);
        guiTipo.setText(null);
        guiAmmount.setText(null);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        guiSearch = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        guiCardList = new JList(listModel);
        jLabel1 = new javax.swing.JLabel();
        guiNome = new javax.swing.JTextField();
        guiCost = new javax.swing.JTextField();
        guiCMC = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        guiTipo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        guiRaridade = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        guiSet = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        guiDescricao = new javax.swing.JTextArea();
        guiSalvar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        guiPoder = new javax.swing.JTextField();
        guiResistencia = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        guiApagar = new javax.swing.JButton();
        guiAtualizaLista = new javax.swing.JButton();
        guiAmmount = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        guiServidor = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        guiPorta = new javax.swing.JTextField();
        guiArquivo = new javax.swing.JTextField();
        guiConectar = new javax.swing.JButton();

        guiCardList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        guiCardList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                guiCardListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(guiCardList);

        jLabel1.setText("Nome");

        jLabel2.setText("Custo de Mana");

        jLabel3.setText("CMC");

        jLabel4.setText("Tipo");

        jLabel5.setText("Raridade");

        jLabel6.setText("Conjunto");

        jLabel7.setText("Descrição");

        guiDescricao.setColumns(20);
        guiDescricao.setRows(5);
        jScrollPane2.setViewportView(guiDescricao);

        guiSalvar.setText("Salvar");
        guiSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiSalvarActionPerformed(evt);
            }
        });

        jLabel8.setText("Poder");

        jLabel9.setText("Resistencia");

        guiApagar.setText("Apagar");
        guiApagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiApagarActionPerformed(evt);
            }
        });

        guiAtualizaLista.setText("Atualizar Lista");
        guiAtualizaLista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiAtualizaListaActionPerformed(evt);
            }
        });

        jLabel10.setText("Quantidade");

        jLabel11.setText("Servidor:");

        guiServidor.setText("localhost");

        jLabel12.setText("Porta:");

        jLabel13.setText("Arquivo:");

        guiArquivo.setText("archiveClient");

        guiConectar.setText("Conectar");
        guiConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiConectarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                            .addComponent(guiSearch))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(guiNome)
                                            .addComponent(guiTipo))
                                        .addGap(6, 6, 6))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel1))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(guiCost, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(guiCMC)))
                            .addComponent(jScrollPane2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(88, 88, 88))
                                    .addComponent(guiSet))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(guiRaridade, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(guiPoder, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(guiResistencia, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addGap(27, 27, 27)
                                        .addComponent(jLabel9))
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(guiAmmount, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(guiConectar)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(guiAtualizaLista)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(guiApagar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(guiSalvar))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel11)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(guiServidor, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel12)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(guiPorta, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel13)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(guiArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 3, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(guiServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(guiPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(guiArquivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guiConectar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guiSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(guiNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(guiCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(guiTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(guiCMC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(1, 1, 1)
                                .addComponent(guiSet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(guiRaridade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(guiPoder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(guiResistencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(guiAmmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guiAtualizaLista)
                    .addComponent(guiApagar)
                    .addComponent(guiSalvar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guiSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiSalvarActionPerformed
        
        botao = true;
        guiSelected = guiCardList.getSelectedIndex();

        if (guiSelected == 0) {
            idCounter = increaseCount();
            String toString = idCounter + " : " + guiNome.getText() + " : " + guiTipo.getText() + " : " + guiCost.getText() + " : " + guiSet.getText() + " : " + guiCMC.getText() + " : " + guiPoder.getText() + " : " + guiResistencia.getText() + " : " + guiRaridade.getText() + " : " + guiDescricao.getText() + " : " + guiAmmount.getText();
            try {
                serverConnect.addCard(toString);
            } catch (Exception ex) {
                Logger.getLogger(GUIClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            Model tempModel = new Model();
            tempModel.setId(idCounter);
            tempModel.setName(guiNome.getText());
            tempModel.setType(guiTipo.getText());
            tempModel.setMana(guiCost.getText());
            tempModel.setSet(guiSet.getText());
            tempModel.setConvMana(Integer.parseInt(guiCMC.getText()));
            tempModel.setPower(Integer.parseInt(guiPoder.getText()));
            tempModel.setTough(Integer.parseInt(guiResistencia.getText()));
            tempModel.setRarity(guiRaridade.getText());
            //tempModel.setFlavor(tempAtt[9].split(" ::: ")[1]);
            tempModel.setFlavor(guiDescricao.getText());
            tempModel.setAmmount(Integer.parseInt(guiAmmount.getText()));

            for (int i = 0; i < serverConnect.getCardList().getCardsList().size(); i++) {
                try {
                    if (tempModel.getName().equals(serverConnect.getCardList().getCardsList().get(i).getName()) && tempModel.equals(serverConnect.getCardList().getCardsList().get(i))) {
                        idCounter = increaseCount();
                        serverConnect.removeCardById(serverConnect.getCardList().getCardsList().get(i).getId());
                        serverConnect.addCard(tempModel.toString());
                    }
                } catch (Exception ex) {
                    Logger.getLogger(GUIClient.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //atualizaLista();
            limpaCampos();
            botao = false;
        }
    }//GEN-LAST:event_guiSalvarActionPerformed

    private void guiAtualizaListaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiAtualizaListaActionPerformed
        // TODO add your handling code here:
        atualizaLista();
    }//GEN-LAST:event_guiAtualizaListaActionPerformed

    private int increaseCount() {
        int maxId = 0;
        for (int i = 0; i < serverConnect.getCardList().getCardsList().size(); i++) {
            if (serverConnect.getCardList().getCardsList().get(i).getId() > maxId) {
                maxId = serverConnect.getCardList().getCardsList().get(i).getId();
            }
        }
        return (maxId + 1);
    }

    private void guiCardListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_guiCardListValueChanged
        // TODO add your handling code here:
        if (botao) {
            botao = false;
            return;
        }
        System.out.println("guivaluechange");
        guiSelected = guiCardList.getSelectedIndex();
        try {
            if (guiSelected != 0) {
                guiCMC.setText(Integer.toString(collection.getCardById((guiSelected - 1)).getConvMana()));
                guiCost.setText(collection.getCardById((guiSelected - 1)).getMana());
                guiDescricao.setText(collection.getCardById((guiSelected - 1)).getFlavor());
                guiNome.setText(collection.getCardByIdLL((guiSelected - 1)).getName());
                guiPoder.setText(Integer.toString(collection.getCardById((guiSelected - 1)).getPower()));
                guiRaridade.setText(collection.getCardById((guiSelected - 1)).getRarity());
                guiResistencia.setText(Integer.toString(collection.getCardById((guiSelected - 1)).getTough()));
                guiSet.setText(collection.getCardById((guiSelected - 1)).getSet());
                guiTipo.setText(collection.getCardById((guiSelected - 1)).getType());
                guiAmmount.setText(Integer.toString(collection.getCardById((guiSelected - 1)).getAmmount()));
            } else {
                guiCMC.setText(null);
                guiCost.setText(null);
                guiDescricao.setText(null);
                guiNome.setText(null);
                guiPoder.setText(null);
                guiRaridade.setText(null);
                guiResistencia.setText(null);
                guiSet.setText(null);
                guiTipo.setText(null);
                guiAmmount.setText(null);
            }
        } catch (Exception e) {
            System.out.println("yuku");
        }

        //atualizaLista();
    }//GEN-LAST:event_guiCardListValueChanged

    private void guiApagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiApagarActionPerformed
        botao = true;
        guiSelected = guiCardList.getSelectedIndex();

        if (guiSelected > 0) {
            try {
                int id = (serverConnect.getCardList().getCardByName(guiNome.getText())).getId();
                System.out.println("tentei " + guiSelected);
                serverConnect.removeCardById(id);
                System.out.println("retornou");
            } catch (Exception ex) {
                Logger.getLogger(GUIClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            System.out.println("Nooooooooooooooooooooooooo");
        }
        atualizaLista();
        limpaCampos();
        botao = false;
    }//GEN-LAST:event_guiApagarActionPerformed

    private void guiConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiConectarActionPerformed
        // TODO add your handling code here:
        boolean erro = false;
        if (guiPorta.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "Por favor, coloque uma porta válida.");
            erro = true;
        }
        if (guiServidor.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "Por favor, coloque um servidor válido.");
            erro = true;
        }
        if (guiArquivo.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "Por favor, coloque um arquivo válido.");
            erro = true;
        }
        try {
            if (Integer.parseInt(guiPorta.getText()) < 2000) {
                JOptionPane.showMessageDialog(null, "A porta deve ser um número maior que 2000");
                erro = true;
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "A porta deve ser um número maior que 2000");
            erro = true;
        }
        
        if(!erro){
        int porta = Integer.parseInt(guiPorta.getText());
        String servidor = guiServidor.getText();
        String arquivo = guiArquivo.getText();
        arquivo += ".txt";
        
        serverConnect = new WebClient(servidor, porta, arquivo);

        atualizaLista();
        
        guiAtualizaLista.setEnabled(true);
        guiApagar.setEnabled(true);
        guiSalvar.setEnabled(true);
        }
    }//GEN-LAST:event_guiConectarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField guiAmmount;
    private javax.swing.JButton guiApagar;
    private javax.swing.JTextField guiArquivo;
    private javax.swing.JButton guiAtualizaLista;
    private javax.swing.JTextField guiCMC;
    private javax.swing.JList guiCardList;
    private javax.swing.JButton guiConectar;
    private javax.swing.JTextField guiCost;
    private javax.swing.JTextArea guiDescricao;
    private javax.swing.JTextField guiNome;
    private javax.swing.JTextField guiPoder;
    private javax.swing.JTextField guiPorta;
    private javax.swing.JTextField guiRaridade;
    private javax.swing.JTextField guiResistencia;
    private javax.swing.JButton guiSalvar;
    private javax.swing.JTextField guiSearch;
    private javax.swing.JTextField guiServidor;
    private javax.swing.JTextField guiSet;
    private javax.swing.JTextField guiTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
